package com.itheima.service.impl;


import com.itheima.mapper.OrdersettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrdersettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.YearMonth;
import java.util.*;

@Service
@Transactional
public class OrdersettingServiceImpl implements OrdersettingService {
    @Autowired
    private OrdersettingMapper orderSettingMapper;

    /**
     * 批量添加预约设置信息
     *
     * @param data
     */

    public void add(List<OrderSetting> data) {

        if (data != null && data.size() > 0) {
            for (OrderSetting orderSetting : data) {
                //判断当前日期是否已经进行了预约设置
                long count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
                if (count > 0) {
                    //已设置  进行更改
                    orderSettingMapper.editNumberByOrderDate(orderSetting);
                } else {
                    //没有进行设置  进行添加
                    orderSettingMapper.add(orderSetting);
                }
            }
        }
    }

    /**
     * 根据日期查询预约设置信息
     *
     * @param month
     * @return
     */
    public List<Map> getOrderSettingByMonth(String month) {
        // 补全月份为两位数，如："2025-6" → "2025-06"
        String[] parts = month.split("-");
        if (parts.length != 2) {
            throw new IllegalArgumentException("日期格式错误，应为 yyyy-MM");
        }
        int year = Integer.parseInt(parts[0]);// 获取年份
        int monthValue = Integer.parseInt(parts[1]);//  获取月份

        // 格式化为两位月份
        String formattedMonth = String.format("%02d", monthValue); // 补零操作
        String fullMonth = year + "-" + formattedMonth;

        // 使用 YearMonth 解析标准格式
        YearMonth yearMonth = YearMonth.parse(fullMonth);

        LocalDate begin = yearMonth.atDay(1);
        LocalDate end = yearMonth.atEndOfMonth();

        Map map = new HashMap<>();
        map.put("dateBegin", begin);
        map.put("dateEnd", end);
        List<OrderSetting> list = orderSettingMapper.getOrderSettingByMonth(map);

        List<Map> data = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                Map result = new HashMap();
                result.put("date", orderSetting.getOrderDate().getDate());//获得日期
                result.put("number", orderSetting.getNumber());//获得可预约人数
                result.put("reservations", orderSetting.getReservations());//获得已预约人数
                data.add(result);
            }
        }
        return data;
    }

    /**
     * 根据日期修改可预约人数
     *
     * @param orderSetting
     */
    public void editNumberByOrderDate(OrderSetting orderSetting)  {

        Date orderDate = orderSetting.getOrderDate();
        //根据日期查询是否已经进行了预约设置
        long count = orderSettingMapper.findCountByOrderDate(orderDate);
        if (count > 0) {
            orderSettingMapper.editNumberByOrderDate(orderSetting);

        } else {
            orderSettingMapper.add(orderSetting);
        }

    }

}
